<?php
namespace Home\Controller;
use Think\Controller;

class PublicController extends Controller {
		
	 public function __construct(){
    	parent::__construct();
    	
         
    }
    
	public function upload()
	{
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =    3145728 ;// 设置附件上传大小    
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      './Company/Logo/'; // 设置附件上传目录
		$upload->saveName = (string)session('user.id');
		$upload->saveExt = 'JPEG';
		$upload->subName = "";
		$upload->replace = true;
		$upload->hash = false;
		$info = $upload->upload();

		if (!$info)
		{
			$msg['code'] = 0;
			$msg['msg'] = $upload->getError();
		} else {
			$msg['code'] = 1;
			$msg['msg'] = '/Uploads/Company/Logo/'.$info['file_logo']['savename'];
		}
		$this->ajaxReturn($msg);
	}

	public function teamUpload()
	{
		$data = I();
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =    3145728 ;// 设置附件上传大小    
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      './Company/Team/'; // 设置附件上传目录
		$upload->saveName = (string)session('user.id').'_'.$data['w'];
		$upload->saveExt = 'JPEG';
		$upload->subName = "";
		$upload->replace = true;
		$upload->hash = false;
		$info = $upload->upload();

		if (!$info)
		{
			$msg['code'] = 0;
			$msg['msg'] = $upload->getError();
		} else {
			$msg['code'] = 1;
			$msg['msg'] = '/Uploads/Company/Team/'.$info['myfiles']['savename'];
		}
		$this->ajaxReturn($msg);
	}

	public function productUpload()
	{
		$data = I();
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =    3145728 ;// 设置附件上传大小    
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      './Company/Product/'; // 设置附件上传目录
		$upload->saveName = (string)session('user.id').'_'.$data['w'];
		$upload->saveExt = 'JPEG';
		$upload->subName = "";
		$upload->replace = true;
		$upload->hash = false;
		$info = $upload->upload();

		if (!$info)
		{
			$msg['code'] = 0;
			$msg['msg'] = $upload->getError();
		} else {
			$msg['code'] = 1;
			$msg['msg'] = '/Uploads/Company/Product/'.$info['myfiles']['savename'];
		}
		$this->ajaxReturn($msg);
	}

    public function getparent($cateid){
        $res=array();
        $cate=D('cate');
        while ($cateid) {
            $cates=$cate->find($cateid);
            $res[]=array(
                'id'=>$cates['id'],
                'name'=>$cates['name'],
                'type'=>$cates['type']
                );
            $cateid=$cates['parentid'];
        }

        return array_reverse($res);
   }

     public function selftop($cateid){
        $cate=D('cate');
        $cates=$cate->find($cateid);
        if($cates['class']==1){
            $selftop=$cates['name'];
        }elseif($cates['class']==2){
            $cates=$cate->where('id='.$cates['parentid'])->find();
            $selftop=$cates['name'];
        }else{
            $cates=$cate->where('id='.$cates['parentid'])->find();
            $cates=$cate->where('id='.$cates['parentid'])->find();
            $selftop=$cates['name'];
        }

        $this->assign('selftop',$selftop);
    }

    public function getcate($cateid){

    	if($cateid){
    		$cate=D('cate');
    		$cates=$cate->find($cateid);
    		$catesons=$cate->where('parentid='.$cates['id'])->select();
    		if($cates['parentid']==0 && !$catesons){
    			$cates=$cate->where("id=68")->find();
    			$this->assign('contro',true);
    		}
    		$this->assign('catesons',$catesons);

    		if($cates['class']==1){
    			$son2=$cate->where('parentid='.$cates['id'])->select();
    			$this->assign('son2',$son2);
    			$this->assign('son3',null);
    			$this->assign('topname',$cates['name']);

    		}

    		if($cates['class']==2){
    			$topcates=$cate->where('id='.$cates['parentid'])->find();
    			$son2=$cate->where('parentid='.$topcates['id'])->select();
    			$son3=$cate->where('parentid='.$cates['id'])->select();
    			$this->assign('son2',$son2);
    			$this->assign('son3',$son3);
    			$this->assign('topname',$topcates['name']);
    		}


    		if($cates['class']==3){
    			$upercates=$cate->where('id='.$cates['parentid'])->find();//三级栏目对应的二级栏目
    			$topcates=$cate->where('id='.$upercates['parentid'])->find();
    			$son3=$cate->where('parentid='.$upercates['id'])->select();
    			$son2=$cate->where('parentid='.$topcates['id'])->select();
    			$this->assign('son2',$son2);
    			$this->assign('son3',$son3);
    			$this->assign('topname',$topcates['name']);
    			$this->assign('son3pid',$upercates['id']);

    		}



    	}



    }

    public function nav(){
    	$cate=D('cate');
    	$where=array('parentid'=>0,'pc'=>1);
    	$navres=$cate->field('id,name,type')->order('sort asc')->where($where)->select();
    	$this->assign('navres',$navres);
    }


}